﻿using GUI.RLAServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GUI
{
    /// <summary>
    /// Interaction logic for PageCreateAchievement.xaml
    /// </summary>
    public partial class PageCreateAchievement : Page
    {
        private IRealLifeService _rlaService;
        private User _user;
        public static List<User> participantList;

        public PageCreateAchievement(User user)
        {
            InitializeComponent();
            LoadCategories();
            cmbCategory.SelectedIndex = 0;
            _user = user;
            _rlaService = new RealLifeServiceClient();
            participantList = new List<User>();
        }

        private void btnAddFriends_Click(object sender, RoutedEventArgs e)
        {
            //This UI takes an action in the constructor, which returns the participant list count
            UIAddFriends addFriends = new UIAddFriends(_user, CountParticipants);

            addFriends.Show();
        }

        /// <summary>
        /// Counts the participants.
        /// This method is called, when the action
        /// </summary>
        /// <param name="friendCount">The friend count.</param>
        private void CountParticipants(int friendCount)
        {
            lblFriendCount.Content = "Valgte deltagere: " + friendCount;
        }

        private void btnCreate_Click(object sender, RoutedEventArgs e)
        {
            if (IsValidInput())
            {
                Achievement ach = new Achievement
                {
                    Title = txtTitle.Text,
                    Tag = txtTags.Text,
                    Description = txtDescription.Text,
                    Category = cmbCategory.SelectedItem.ToString(),
                    CurrentStatus = Achievement.Status.Active,
                    Criteria = "not yet implemented",
                    Participants = participantList//.ToArray() Kommentar af jan
                };

                int result = _rlaService.CreateAchievement(_user, ach);
                if (result < 0)
                {
                    MessageBox.Show("Something went wrong, dude! Error code: " + result);
                }
                else
                {
                    MessageBox.Show("Achievement blev oprettet!");
                    ClearInput();
                }
            }
        }

        private void btnCancel_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("hvad skal jeg gøre???");
        }

        private void cmbCategory_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
        }

        private void LoadCategories()
        {
            cmbCategory.Items.Add("Kategori1");
            cmbCategory.Items.Add("Kategori2");
            cmbCategory.Items.Add("Kategori3");
        }

        private bool IsValidInput()
        {
            string[] args = { txtTitle.Text, txtDescription.Text, txtTags.Text };
            bool valid = true;
            int index = 0;
            while (valid && index < args.Count())
            {
                if (args[index].Length < 1)
                {
                    valid = false;
                }

                index++;
            }
            if (!valid)
                MessageBox.Show("Input is missing in one of your nice text fields!");
            return valid;
        }

        private void ClearInput()
        {
            txtDescription.Text = "";
            txtTitle.Text = "";
            txtTags.Text = "";
        }
    }
}